package ru.spbau.database.group3.queryEvaluator;

import ru.spbau.database.group3.engine.Condition;
import ru.spbau.database.group3.engine.DatabaseEngine;
import ru.spbau.database.group3.parser.Constraint;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: stas
 * Date: 1/29/13
 * Time: 2:02 PM
 */
public class SelectInstruction implements Instruction {

    public SelectInstruction(String tableName, List<Constraint> constraints, List<String> colNames) {
        this.tableName = tableName;
        this.conditions = createConditions(constraints);
        this.colNames = colNames;
    }

    private List<Condition> createConditions(List<Constraint> constraints) {
        List<Condition> conditions = new ArrayList<Condition>();
        for(Constraint c : constraints) {
            conditions.add(DatabaseEngine.createCondition(tableName,c));
        }
        return conditions;
    }

    @Override
    public QueryResult handle(DatabaseEngine engine) throws IOException, ClassNotFoundException {
        return engine.select(tableName, colNames, conditions);
    }

    private String tableName;
    private List<Condition> conditions;
    private List<String> colNames;
}
